package com.ly.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ly.common.core.bean.R;
import com.ly.common.core.bean.dto.CommonBatchUpdateDTO;
import com.ly.common.core.bean.dto.MsgDTO;
import com.ly.system.service.ISysCommonService;
import com.ly.system.system.bean.entity.SysMessageTemplate;
import com.ly.system.service.ISysMessageTemplateService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 系统消息模板
 *
 * @author luoyun
 * @Sate: 2019-04-09
 */
@Tag(name = "消息模版")
@Slf4j
@RestController
@RequestMapping("/sys/msgTemplate")
@RequiredArgsConstructor
public class SysMessageTemplateController {

    private final ISysMessageTemplateService sysMessageTemplateService;
    private final ISysCommonService sysCommonService;

    /**
     * 查询-id
     *
     * @param id
     * @return
     */
    @Operation(summary = "查询-id")
    @GetMapping("/getId")
    public R<SysMessageTemplate> getId(@RequestParam(name = "id") String id) {
        return sysMessageTemplateService.getId(id);
    }

    /**
     * 列表
     *
     * @param entity
     * @return
     */
    @Operation(summary = "列表")
    @GetMapping("/getList")
    public R<List<SysMessageTemplate>> getList(SysMessageTemplate entity) {
        return sysMessageTemplateService.getList(entity);
    }

    /**
     * 分页
     *
     * @param entity
     * @param pageNo
     * @param pageSize
     * @return
     */
    @Operation(summary = "分页")
    @GetMapping("/getPage")
    public R<IPage<SysMessageTemplate>> getPage(
            SysMessageTemplate entity,
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        return sysMessageTemplateService.getPage(entity, new Page<SysMessageTemplate>(pageNo, pageSize));
    }

    /**
     * 新增
     *
     * @param entity
     * @return
     */
    @Operation(summary = "新增")
    @PostMapping("/add")
    public R<String> add(@RequestBody SysMessageTemplate entity) {
        return sysMessageTemplateService.add(entity);
    }

    /**
     * 修改
     *
     * @param entity
     * @return
     */
    @Operation(summary = "修改")
    @PostMapping("/edit")
    public R<String> edit(@RequestBody SysMessageTemplate entity) {
        return sysMessageTemplateService.edit(entity);
    }

    /**
     * 删除-id
     *
     * @param id
     * @return
     */
    @Operation(summary = "删除-id")
    @DeleteMapping("/delete")
    public R<String> delete(@RequestParam(name = "id") String id) {
        return sysMessageTemplateService.delete(id);
    }

    /**
     * 批量新增或修改
     *
     * @param dto
     * @return
     */
    @Operation(summary = "批量新增或修改")
    @PostMapping("/updateBatch")
    public R updateBatch(@Validated @RequestBody CommonBatchUpdateDTO<SysMessageTemplate> dto) {
        return sysMessageTemplateService.updateBatch(dto);
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @Operation(summary = "批量删除")
    @DeleteMapping("/deleteBatch")
    public R<String> deleteBatch(@RequestParam(name = "ids") String ids) {
        return sysMessageTemplateService.deleteBatch(ids);
    }

    /**
     * 发送消息
     */
    @PostMapping("/sendMsg")
    public R<String> sendMessage(@RequestBody MsgDTO dto) {
        dto.setToAll(false);
        dto.setTitle("消息发送测试");
        return sysCommonService.sendMsg(dto);
    }
}
